package test.test1.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import test.test1.VelocityKey;
import test.test1.mybatis.pojo.Player;
import test.test1.mybatis.service.PlayerService;



@Controller
@RequestMapping("/player")
public class PlayerController {
	@Resource
	private PlayerService playerService;

	public PlayerController() {
		// TODO Auto-generated constructor stub
	}
	
	@RequestMapping("addPlayer")
	public String save(HttpServletRequest request, ModelMap modelMap) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String nickname = request.getParameter("nickname");
		Player player = new Player();
		player.setName(nickname);
		player.setPassword(password);
		player.setScore(0);
		player.setUsername(username);
		String msg = "";
		try {
			int i = playerService.addPlayer(player);
			if(i == 1) {
				msg = "Add is done. " + player;
			} else {
				msg = "Add fail for unknown";
			}
		} catch(Exception e) {
			msg = e.getMessage();
			e.printStackTrace();
		}
		modelMap.put(VelocityKey.MESSAGE, msg);
		return "message";
	}
		
	@RequestMapping("list")
	public String list(HttpServletRequest request, ModelMap modelMap) {
		List<Player> players = playerService.getAllPlayers();
		modelMap.put(VelocityKey.THELIST, players);
		return "list";
	}

}
